/*
 * Copyright ©2021 su binglun(9085309@qq.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *---------------------------------------------------------------------
 *-                                                                   -
 *-           Website : http://phoenix.sunny360.cn/                   -
 *-                                                                   -
 *---------------------------------------------------------------------
 */
package cn.sunny360.core.verticle;

import cn.sunny360.core.common.HttpHandlerMapping;
import cn.sunny360.core.common.MsgActionMapping;
import cn.sunny360.core.handler.msg.*;
import cn.sunny360.core.service.*;
import cn.sunny360.core.service.impl.*;
import cn.sunny360.core.utils.Container;
import cn.sunny360.core.utils.FileUtils;
import cn.sunny360.core.wrapper.CacheWrapper;
import cn.sunny360.core.wrapper.DBWrapper;
import cn.sunny360.core.wrapper.impl.ConcurrentMapWrapper;
import cn.sunny360.core.wrapper.impl.MysqlClientWrapper;
import com.sohu.idcenter.IdWorker;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.json.JsonObject;


/**
 * A verticle publishing the user service.
 *
 * @author sublun
 */
public class MainVerticle extends BaseVerticle {

	private static final Logger logger = LoggerFactory.getLogger(MainVerticle.class);

	@Override
	public void start() throws Exception {
		super.start();
		//初始化系统组件
		initComponent();
		deployVerticles();
		logger.info("系统已经启动...");
	}

	private void initComponent() {
		//Tools
		//IdWorker(long workerId, long datacenterId, long sequence)
		//参数1：机器编号 参数2：数据中心编号 参数3：序列起始值
		//如果有需要多个数据服务生成id则需要配置不同的机器编号及数据中心编号，不能大于31
		//在不同的服务中使用IDWorker最好设置不同的数据中心编号，也就是第二个参数值，不能大于31
		IdWorker idWorker = new IdWorker(1, 1, 0);
		Container.putComponent(IdWorker.class, idWorker);

		//Wrappers
		DBWrapper dbWrapper = new MysqlClientWrapper(vertx, config());
		Container.putComponent(DBWrapper.class, dbWrapper);
		CacheWrapper cacheWrapper = new ConcurrentMapWrapper(vertx);
		Container.putComponent(CacheWrapper.class, cacheWrapper);

		//Service
		UserService userService = new UserServiceImpl();
		Container.putComponent(UserService.class, userService);
		DeviceService deviceService = new DeviceServiceImpl();
		Container.putComponent(DeviceService.class, deviceService);
		MessageService messageService = new MessageServiceImpl();
		Container.putComponent(MessageService.class, messageService);
		LinkmanService linkmanService = new LinkmanServiceImpl();
		Container.putComponent(LinkmanService.class, linkmanService);
		SystemService systemService = new SystemServiceImpl();
		Container.putComponent(SystemService.class, systemService);
		GroupService groupService = new GroupServiceImpl();
		Container.putComponent(GroupService.class, groupService);

		//HttpHandlerMapping
		HttpHandlerMapping restHandlerMapping = new HttpHandlerMapping()
				.addHandler("post", "/user/login", "cn.sunny360.core.handler.http.UserHandler#apiUserLogin")
				.addHandler("post", "/linkman/list", "cn.sunny360.core.handler.http.LinkmanHandler#getUserLinkmanList")
				.addHandler("post", "/linkman/add", "cn.sunny360.core.handler.http.LinkmanHandler#addUserLinkman")
				.addHandler("post", "/linkman/remove", "cn.sunny360.core.handler.http.LinkmanHandler#removeUserLinkman")
				.addHandler("post", "/linkman/uptime", "cn.sunny360.core.handler.http.LinkmanHandler#updateUserLinkTime")
				.addHandler("get" , "/user/id/:id", "cn.sunny360.core.handler.http.UserHandler#getUserInfoById")
				.addHandler("get" , "/user/name/:username", "cn.sunny360.core.handler.http.UserHandler#getUserInfoByName")
				.addHandler("post" , "/user/create", "cn.sunny360.core.handler.http.UserHandler#createUser")
				.addHandler("get" , "/user/role/:roleId", "cn.sunny360.core.handler.http.UserHandler#getUserListByRole")
				.addHandler("post" , "/user/update", "cn.sunny360.core.handler.http.UserHandler#updateUser")
				.addHandler("post", "/device/register", "cn.sunny360.core.handler.http.DeviceHandler#deviceRegister")
				.addHandler("post", "/group/list", "cn.sunny360.core.handler.http.GroupHandler#getGroupList")
				.addHandler("post", "/group/create", "cn.sunny360.core.handler.http.GroupHandler#createGroup")
				.addHandler("post", "/group/apply/join", "cn.sunny360.core.handler.http.GroupHandler#applyJoinGroup")
				.addHandler("post", "/group/add/member", "cn.sunny360.core.handler.http.GroupHandler#addGroupMember")
				.addHandler("post", "/group/approved/apply", "cn.sunny360.core.handler.http.GroupHandler#approvedApply")
				.addHandler("post", "/group/member/list", "cn.sunny360.core.handler.http.GroupHandler#getGroupMemberList")
				.addHandler("get", "/group/apply/list/:groupId", "cn.sunny360.core.handler.http.GroupHandler#getApplyList")
				;
		Container.putComponent("restHandlerMapping", restHandlerMapping);

		//MsgActionMapping
		MsgActionMapping msgActionMapping = new MsgActionMapping()
				.putAction("connection", new ConnectionAction())
				.putAction("message", new MessageAction())
				.putAction("sync", new SyncAction())
				.putAction("ack", new AckAction())
				.putAction("history", new HistoryAction())
				;
		Container.putComponent(MsgActionMapping.class, msgActionMapping);
	}

	private void deployVerticles() {
		//Verticles
		DeploymentOptions options = new DeploymentOptions().setConfig(config());
		deployVerticle(RestApiVerticle.class, options);
		deployVerticle(ChatVerticle.class, options);
	}


	public static void main(String[] args) {
		Vertx vertx = Vertx.vertx();
		JsonObject config = FileUtils.getConfig(vertx.fileSystem());
		logger.debug(config);
		DeploymentOptions options = new DeploymentOptions().setConfig(config);
		vertx.deployVerticle(MainVerticle.class, options);
	}

}
